package com.projectx.action.contact;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.DividerItemDecoration;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.content.DialogInterface;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

import com.projectx.R;
import com.projectx.util.ActivityManage;

public class CreateGroupActivity extends AppCompatActivity {
   // private List<String> data=new ArrayList<>();
   // private List<Bitmap> bitmaps=new ArrayList<>();
    protected static RecyclerView recyclerView_horizontal;
    protected static RecyclerView recyclerView;
    protected static WindowManager windowManager;
    protected static EditText editText;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_create_group);
        ActivityManage.addActivity("CreateGroupActivity",this);
        try {
            getSupportActionBar().hide(); //去除标题栏
        } catch (NullPointerException e){
            Log.d("KMH","没有状态栏标题");
        }
        recyclerView_horizontal =findViewById(R.id.recycler_view_horizontal);
        recyclerView=findViewById(R.id.recycler_view);
        final ImageView back=findViewById(R.id.back);
        final Button create=findViewById(R.id.create);
        editText=findViewById(R.id.edittext);

        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ActivityManage.exitActivity("CreateGroupActivity");
            }
        });
        create.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (recyclerView_horizontal.getAdapter().getItemCount()==0)
                    Toast.makeText(CreateGroupActivity.this,"请选择群聊成员",Toast.LENGTH_SHORT).show();
                else {
                    final AlertDialog.Builder alertDialog=new AlertDialog.Builder(v.getContext());
                    alertDialog.setTitle("群聊名称");
                    final EditText editText1=new EditText(v.getContext());
                    editText1.setHint("请填写群聊名称");
                    alertDialog.setView(editText1);
                    alertDialog.setPositiveButton("确认", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            boolean flag=true;
                            for (int i=0;i<TabFragment.data_group.size();i++){
                                if (editText1.getText().toString().equals(TabFragment.data_group.get(i))){
                                    Toast.makeText(CreateGroupActivity.this,"该群聊名称已被使用",Toast.LENGTH_SHORT).show();
                                    flag=false;
                                    break;
                                }
                            }
                            if (flag==true){
                                TabFragment.data_group.add(editText1.getText().toString());
                                TabFragment.bitmaps.add(null);
                                ActivityManage.exitActivity("CreateGroupActivity");
                            }
                        }
                    });
                    alertDialog.setNegativeButton("取消",null);
                    alertDialog.show();
                }
                //创建群聊
            }
        });
        LinearLayoutManager linearLayoutManager=new LinearLayoutManager(this);
        linearLayoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
        recyclerView_horizontal.setLayoutManager(linearLayoutManager);
        HorizontalAdapter horizontal_adapter=new HorizontalAdapter(this,TabFragment.bitmaps,TabFragment.data_person);
        recyclerView_horizontal.setAdapter(horizontal_adapter);
        recyclerView_horizontal.addItemDecoration(new DividerItemDecoration(this, DividerItemDecoration.HORIZONTAL));
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        final CreateGroupAdpter createGroupAdpter=new CreateGroupAdpter(this,TabFragment.data_person,TabFragment.bitmaps);
        recyclerView.setAdapter(createGroupAdpter);
        recyclerView.addItemDecoration(new DividerItemDecoration(this,DividerItemDecoration.VERTICAL));
        windowManager=this.getWindowManager();
        editText.addTextChangedListener(new TextWatcher() {
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            }//文本改变之前执行

            @Override
            //文本改变的时候执行
            public void onTextChanged(CharSequence s, int start, int before, int count) {
               // data.add("");
                createGroupAdpter.notifyDataSetChanged();
                Toast.makeText(CreateGroupActivity.this,"没有数据",Toast.LENGTH_SHORT).show();
                //如果长度为0
                if (s.length() == 0) {
                    //隐藏“删除”图片
                    //delete.setVisibility(View.GONE);
                    recyclerView.setVisibility(View.GONE);
                } else {//长度不为0
                    //显示“删除图片”
                    //delete.setVisibility(View.VISIBLE);
                    recyclerView.setVisibility(View.VISIBLE);
                    //text=editText.getText().toString();
                }
            }

            public void afterTextChanged(Editable s) {
            }//文本改变之后执行
        });
    }
}
